<template>
  <div class="goods-sku">
    <dl v-for="item in goods.specs" :key="item.id">
      <dt>{{ item.name }}</dt>
      <dd>
        <template v-for="val in item.values">
          <img v-if="val.picture" :key="val.name" :class="{selected:val.selected}" :src="val.picture" alt="" @click="changeSku(item,val)">
          <span v-else :key="val.name" :class="{selected:val.selected}" @click="changeSku(item,val)">{{ val.name }}</span>
        </template>
      </dd>
    </dl>
  </div>
</template>
<script>
const getPathMap = () => {

}

export default {
  name: 'GoodsSku',
  props: {
    goods: {
      type: Object,
      default: () => ({})
    }
  },
  setup(props) {
    const pathMap = getPathMap(props.goods.skus)
    console.log(pathMap)

    const changeSku = (item, val) => {
      /**
       * 排他思想：
       * 当有选中变成未选中状态时，直接修改
       * 由未选中变成选中时，先将所有的状态设置为未选中，然后将当前项变为选中。
       */
      if (val.selected) {
        val.selected = false
      } else {
        item.values.forEach(ele => {
          ele.selected = false
        })
        val.selected = true
      }
    }
    return { changeSku }
  }
}
</script>
<style scoped lang="less">
.sku-state-mixin () {
  border: 1px solid #e4e4e4;
  margin-right: 10px;
  cursor: pointer;
  &.selected {
    border-color: @xtxColor;
  }
  &.disabled {
    opacity: 0.6;
    border-style: dashed;
    cursor: not-allowed;
  }
}
.goods-sku {
  padding-left: 10px;
  padding-top: 20px;
  dl {
    display: flex;
    padding-bottom: 20px;
    align-items: center;
    dt {
      width: 50px;
      color: #999;
    }
    dd {
      flex: 1;
      color: #666;
      > img {
        width: 50px;
        height: 50px;
        .sku-state-mixin ();
      }
      > span {
        display: inline-block;
        height: 30px;
        line-height: 28px;
        padding: 0 20px;
        .sku-state-mixin ();
      }
    }
  }
}
</style>
